//
//  APPTextViewAlertViewController.swift
//  APP
//
//  Created by Rdxer on 2017/12/20.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit


/// 提供一个 TextView 输入框的
open class APHTextViewAlertViewController: APHAlertViewController {
    let textViewH = 120
    
    open var textView = LXFPlaceholderTextView.init().then {
        $0.placeholder = "..."
        $0.placeholderColor = AppColor.lightTextColor
        $0.layer.cornerRadius = 3
        $0.clipsToBounds = true
        $0.font = 14.toFont
        $0.textColor = AppColor.textColor
        $0.backgroundColor = AppColor.backgroundColor
    }
    
    deinit {
        removeListenKeyboard()
    }
    
    open override func prepareSubViews(){
        prepareAddListenKeyboard()
        
        view.addSubviews([
            backView,
            containerView.addSubviews([
                title_Label,
                textView,
                buttonsContainerView.addSubviews([
                    cancelButton,
                    confirmButton
                    ])
                ])
            ])
        
    }
    
    open override func layoutSubViews(){
        
        backView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        containerView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(w)
        }
        
        title_Label.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(titleHeight)
        }
        
        buttonsContainerView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(rowHeight)
        }
        
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(title_Label.snp.bottom).offset(margin)
            make.bottom.equalTo(buttonsContainerView.snp.top).offset(-margin)
            make.left.equalToSuperview().offset(margin)
            make.right.equalToSuperview().offset(-margin)
            make.height.equalTo(textViewH)
        }
        
        buttonsContainerView.xx_HorizontalTile(withSubviews: [cancelButton,confirmButton], insets: .zero)
    }
}

extension APHTextViewAlertViewController{
    func prepareAddListenKeyboard(){
        NotificationCenter.default.addObserver(self, selector: #selector(xx_UIKeyboardWillChangeFrame(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    }
    
    func removeListenKeyboard(){
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    }
    /// 480
    @objc func xx_UIKeyboardWillChangeFrame(notification: NSNotification){
        let info = notification.userInfo
        let kbRect = (info?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        
        let offsetY = (480.from1080) + kbRect.origin.y - UIScreen.main.bounds.height
        
        let y = XXMathLibrary.absLimit(offsetY, min: -9999, max: 0)
        UIView.animate(withDuration: 0.3) {
            self.view.transform = CGAffineTransform(translationX: 0, y: y)
        }
    }
    
}


